import dd from 'gdt-jsapi';
import api from '../../api/index'
import {formatTs} from '../../common/utils'
import {getUserInfo} from '../../common/common'

Page({
  data: {
    userInfo: {},
    ApplyStatus: {
      Pending: 1,
      Passed: 2,
      Rejected: 3,
      Canceled: 4,
    },
    ApproveStatus: {
      Passed: 1,
      Rejected: 2,
    },
    applyTabIndex: 1,
    applyList: [],
    personCacheObj: {}
  },
  async getPersonList() {
    try {
      const res = await api.getPersonList()
      if (res.data && res.data.length) {
        res.data.forEach((p) => {
          this.data.personCacheObj[p.accountId] = p
        })
      }
    } catch(e){

    }
  },
  async onLoad(options) {
    await this.getPersonList()
    this.data.userInfo = await getUserInfo()
    //this.data.userInfo = {accountId: '8825254'}
    const tab = options.tab
    if (tab) {
      this.onApplyTabClick(null, Number(tab))
    } else {
      this.getApplyList()
    }
  },
  async getApplyDetail(id){
    try {
      const res = await api.getApplyDetail(id)
      if (res.data) {
        return res.data
      }
    } catch(e){
      return null
    }
  },
  async getApplyList(){
    dd.showLoading({
      text: '加载中'
    })
    if (!this.data.userInfo.accountId) {
      this.data.userInfo = await getUserInfo()
    }
    api.getApplyList({
      pageIndex: 1,
      pageSize: 100,
      applyAccountId: this.data.userInfo.accountId,
      status: this.data.applyTabIndex
    }).then((res) => {
      if (res.data && res.data.data) {
        res.data.data.forEach((apply) => {
          apply.approveList = []
          apply.applyTimeStr = formatTs(Number(apply.applyTime))
          apply.approvePersonNames = ''
          if (apply.approveAccountIdList) {
            const approve = []
            const approveArr = apply.approveAccountIdList.split(',')
            approveArr.forEach((id) => {
              if (id && this.data.personCacheObj[id]) {
                approve.push(this.data.personCacheObj[id].name)
              }
            })
            apply.approvePersonNames = approve.join('，')
          }
        })
        this.setData({
          applyList: res.data.data,
        })
      }
      dd.hideLoading()
    }).catch((e) => {
      dd.hideLoading()
    })
  },
  onApplyTabClick(e, i){
    let index = i
    if (e) {
      index = Number(e.target.dataset.type)
    }
    this.setData({
      applyList: [],
      applyTabIndex: index
    })
    this.getApplyList()
  },
  async onViewDetail(e){
    dd.showLoading({
      text: '加载中'
    })
    const index = Number(e.target.dataset.index)
    const item = this.data.applyList[index]
    const data = await this.getApplyDetail(item.id)
    dd.hideLoading()
    if (data && data.approveList.length) {
      data.approveList.forEach((approve) => {
        approve.approveTimeStr = formatTs(Number(approve.approveTime))
      })
      item.approveList = data.approveList
      this.setData({
        ['applyList['+index+']']: item
      })
    } else {
      dd.showToast({
        content: '暂无审批记录',
        type: 'none'
      })
    }
  },
  exeCancelApply(id){
    dd.showLoading({
      text: '加载中'
    })
    api.cancelApply(id).then((res) => {
      dd.hideLoading()
      if (res.code === 0) {
        dd.showToast({
          content: '取消申请成功',
          type: 'success'
        })
        this.getApplyList()
      } else {
        dd.showToast({
          content: res.message || '取消失败',
          type: 'fail'
        })
      }
    }).catch((e) => {
      dd.hideLoading()
      dd.showToast({
        content: '服务异常',
        type: 'exception'
      })
    })
  },
  async onCancelApply(e){
    const item = e.target.dataset.item
    dd.confirm({
      title: "温馨提示",
      message: `确定要取消【${item.applyUseDate} ${item.applyUseTimeRange}】使用【${item.deviceInfo.deviceName}】的申请吗?`,
      buttonLabels: [
        "确定",
        "取消"
      ],
    }).then(res => {
      if (res.buttonIndex === 0) {
        this.exeCancelApply(item.id)
      }
    }).catch(err => {})
  },
});
